package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.ClazzCountOption;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public PageResult<Student> queryStudents(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<Student> students = (Page<Student>) studentMapper.queryAllStudent(name, degree, clazzId);
        PageResult<Student> pageResult = new PageResult<>(students.getTotal(), students.getResult());
        return pageResult;
    }

    @Override
    public void deleteStudent(List<Integer> ids) {
        studentMapper.deleteStudent(ids);
    }

    @Override
    public void insertStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);


    }

    @Override
    public Student queryStudentById(Integer id) {
        Student student = studentMapper.queryStudentById(id);
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        return student;
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);
        student.setUpdateTime(LocalDateTime.now());

    }

    @Override
    public void updateStudentViolation(Integer id, Short score) {
        studentMapper.updateStudentViolation(id, score);

    }

    @Override
    public List<Map<String, Object>> calcStudentDegreeData() {
        List<Map<String, Object>> list = studentMapper.calcStudentDegreeData();
        return list;
    }

    @Override
    public ClazzCountOption calcStudentDegreeData2() {
        List<Map<String, Object>> list = studentMapper.calcStudentDegreeData2();
        List<Object> count = list.stream().map((Map<String, Object> map) -> {
            return map.get("dataList");
        }).collect(Collectors.toList());
        List<Object> clazzName = list.stream().map((Map<String, Object> map) -> {
            return map.get("clazzList");
        }).collect(Collectors.toList());

        return new ClazzCountOption(clazzName, count);
    }
}
